import { Local } from '@/utils/storage';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { storeToRefs } from 'pinia';
import pinia from '@/store/index';
import { User } from '@/store/user';
const userinfo = User(pinia);
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    meta: { title: 'home' },
    component: () => import('@/views/newHome/index.vue'),
  },
  {
    path: '/customer', // 聊天室
    name: 'customer',
    meta: { title: '' },
    component: () => import('@/views/home/customer.vue'),

  },
  {
    path: '/gameDetail',
    name: 'gameDetail',
    meta: { title: 'gameDetail' },
    component: () => import('@/views/gameDetail/index.vue'),
  },
  {
    path: '/game',
    name: 'game',
    meta: { title: 'game' },
    component: () => import('@/views/game/index.vue'),
  },
  {
    path: '/gameCollection',
    name: 'gameCollection',
    meta: { title: 'gameCollection' },
    component: () => import('@/views/gameCollection/index.vue'),
  },
  {
    path: '/openGame',  //游戏页面
    name: 'openGame',
    meta: { title: 'openGame' },
    component: () => import('@/views/openGame/index.vue'),
  },
  {
    path: '/help/:type',
    name: 'helpCenter',
    meta: { title: '帮助中心' },
    component: () => import('@/views/help/index.vue'),
  },
  {
    path: '/club',
    name: 'club',
    meta: { title: 'home_page_club' },
    component: () => import('@/views/club/newClub.vue'),
  },
  {
    path: '/clubNext',
    name: 'clubNext',
    meta: { title: 'home_page_club' },
    component: () => import('@/views/club/clubNext.vue'),
  },
  {
    path: '/ranking',  //赛事排行
    name: 'ranking',
    meta: { title: 'ranking' },
    component: () => import('@/views/ranking/index.vue'),
  },
  {
    path: '/recentAwards',  //近期大奖
    name: 'recentAwards',
    meta: { title: 'recentAwards' },
    component: () => import('@/views/recentAwards/index.vue'),
  },


  {
    path: '/wallet',  //钱包
    name: 'wallet',
    redirect: '/wallet/walletInfo',
    meta: { title: 'wallet' },
    component: () => import('@/views/wallet/index.vue'),
    children: [
      {
        path: '/wallet/walletInfo',
        name: 'walletInfo',
        meta: { title: 'walletInfo' },
        component: () => import('@/views/wallet/walletInfo/index.vue'),
      },
      {
        path: '/wallet/securitySettings',
        name: 'securitySettings',
        meta: { title: 'home_page_securitySettings' },
        component: () => import('@/views/wallet/securitySettings/index.vue'),
      },
      {
        path: '/wallet/myPromo',
        name: 'myPromo',
        meta: { title: 'deposit_page_discount' },
        component: () => import('@/views/wallet/promo/index.vue'),
      },
      {
        path: '/wallet/paymentManagement',
        name: 'paymentManagement',
        meta: { title: 'payment_title' },
        component: () => import('@/views/wallet/paymentManagement/index.vue'),
      },
      {
        path: '/wallet/activity',
        name: 'activity',
        meta: { title: 'home_page_activity' },
        component: () => import('@/views/wallet/activity/index.vue'),
      },
      {
        path: '/wallet/myEmail',
        name: 'myEmail',
        meta: { title: 'myEmail' },
        component: () => import('@/views/wallet/myEmail/index.vue'),
      },
      {
        path: '/wallet/deposit',
        name: 'deposit',
        meta: { title: 'deposit_page_deposit' },
        component: () => import('@/views/wallet/deposit/depositFirst.vue'),
      },
      {
        path: '/wallet/withdraw',
        name: 'withdraw',
        meta: { title: 'proxy_page_withDraw' },
        component: () =>
          import('@/views/wallet/withdrawFunds/withdrawMoney.vue'),
      },
      {
        path: '/wallet/levelInfo',
        name: 'levelInfo',
        meta: { title: 'VIP特权' },
        component: () => import('@/views/wallet/level/levelInfo.vue'),
      },
      {
        path: '/wallet/feedback',
        name: 'feedback',
        meta: { title: 'complaint_feedback' },
        component: () => import('@/views/wallet/feedback.vue'),
      },
      {
        path: '/wallet/levelRule',
        name: 'levelRule',
        meta: { title: 'levelRule' },
        component: () => import('@/views/wallet/level/rules.vue'),
      },
      {
        path: '/wallet/records',
        name: 'records',
        meta: { title: 'records' },
        component: () => import('@/views/wallet/records/index.vue'),
      },
      {
        path: '/wallet/proxyCooperation',
        name: 'proxyCooperation',
        meta: { title: 'proxy_page_agent' },
        component: () => import('@/views/wallet/proxyCooperation/index.vue'),
      },
      {
        path: '/wallet/proxyIntroduction',
        name: 'proxyIntroduction',
        meta: { title: 'proxy_page_agentRule' },
        component: () => import('@/views/wallet/proxyIntroduction/index.vue'),
      },

    ],
  },
];
const router = createRouter({
  routes,
  history: createWebHistory(),
});

const waitForCondition = (next: any, isNext: boolean) => {
  const { loadingEnd } = storeToRefs(userinfo);
  return new Promise(async () => {
    let timer
    if (loadingEnd.value) {
      clearTimeout(timer)
      timer = null
      if (isNext) {
        next(isNext)
      } else {
        await User(pinia).setLogin(true)
        next('/')
      }

    } else {
      timer = setTimeout(() => {
        console.log(55555);

        waitForCondition(next, isNext);
      }, 500);
    }
  })
}

router.beforeEach(async (to: any, _from: any, next: any) => {
  if (Local.get('user')) {
    await waitForCondition(next, true)
  } else {
    if (['home', 'gameMain', 'proxyIntroduction', 'gamingPlatform', 'gameRecords', 'gameDetail', 'activity', 'customer'].includes(to.name)) {
      next()
    } else {
      next(false)
      await User(pinia).setLogin(true)
    }
  }
})
router.afterEach(() => {
  try {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  } catch {
    console.error('滚动到顶部失败');
  }
});
export default router;
